package solution._0100._0108;

import common.TreeNode;

/**
 * Created by chen_snow
 * Date: 2021/7/3 16:55
 * Description: 将有序数组转换为二叉搜索树
 */
public class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        return buildBST(nums, 0, nums.length - 1);
    }

    public TreeNode buildBST(int[] nums, int start, int end) {
        if (end < start) {
            return null;
        }
        int middle = start + (end - start) / 2;
        TreeNode node = new TreeNode(nums[middle]);
        node.left = buildBST(nums, start, middle - 1);
        node.right = buildBST(nums, middle + 1, end);
        return node;
    }
}
